// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

suite("cast_to_datetime") {
    sql "set debug_skip_fold_constant = true"
    sql "set enable_strict_cast = false"

qt_sql1 """ select cast("2023-07-16T19:20:30.123+08:00" as datetime(6)) """
qt_sql2 """ select cast("2023-07-16T19+08:00" as datetime(6)) """
qt_sql3 """ select cast("2023-07-16T1920+08:00" as datetime(6)) """
qt_sql4 """ select cast("70-1-1T00:00:00-0000" as datetime(6)) """
qt_sql5 """ select cast("19991231T235959.5UTC" as datetime(6)) """
qt_sql6 """ select cast("2024-05-01T00:00Asia/Shanghai" as datetime(6)) """
qt_sql7 """ select cast("20231005T081530Europe/London" as datetime(6)) """
qt_sql8 """ select cast("20230105T081530 Europe/London" as datetime(6)) """
qt_sql9 """ select cast("85-12-25T000000gMt" as datetime(6)) """
qt_sql10 """ select cast("2024-05-01" as datetime(6)) """
qt_sql11 """ select cast("24-5-1" as datetime(6)) """
qt_sql12 """ select cast("2024-05-01 0:1:2.333" as datetime(6)) """
qt_sql13 """ select cast("2024-05-01 0:1:2." as datetime(6)) """
qt_sql14 """ select cast("20240501 01" as datetime(6)) """
qt_sql15 """ select cast("20230716 1920Z" as datetime(6)) """
qt_sql16 """ select cast("20240501T0000" as datetime(6)) """
qt_sql17 """ select cast("2024-12-31 23:59:59.9999999" as datetime(6)) """
qt_sql18 """ select cast("2025/06/15T00:00:00.99999999999999" as datetime(6)) """
qt_sql19 """ select cast("2025/06/15T00:00:00.9999987" as datetime(6)) """
qt_sql20 """ select cast("2025/06/15T00:00:00.99999849" as datetime(6)) """
qt_sql21 """ select cast("2020-12-12 13:12:12-03:00" as datetime(6)) """
qt_sql22 """ select cast("0023-01-01T00:00Z" as datetime(6)) """
qt_sql23 """ select cast("69-12-31" as datetime(6)) """
qt_sql24 """ select cast("70-01-01" as datetime(6)) """
qt_sql25 """ select cast("230102" as datetime(6)) """
qt_sql26 """ select cast("19230101" as datetime(6)) """
qt_sql27 """ select cast("120102030405" as datetime(6)) """
qt_sql28 """ select cast("20120102030405" as datetime(6)) """
qt_sql29 """ select cast("20120102T030405" as datetime(6)) """
qt_sql30 """ select cast("120102030405.999" as datetime(6)) """
qt_sql31 """ select cast("2024-05-01 0:1:2.333" as datetime(6)) """
qt_sql32 """ select cast("2023-07-16T19.123+08:00" as datetime(6)) """
qt_sql33 """ select cast("2023-07-16T19+08:00" as datetime(6)) """
qt_sql34 """ select cast("2024/05/01" as datetime(6)) """
qt_sql35 """ select cast("24012" as datetime(6)) """
qt_sql36 """ select cast("2411 123" as datetime(6)) """
qt_sql37 """ select cast("2024-05-01 01:030:02" as datetime(6)) """
qt_sql38 """ select cast("10000-01-01 00:00:00" as datetime(6)) """
qt_sql39 """ select cast("2024-0131T12:00" as datetime(6)) """
qt_sql40 """ select cast("2024-05-01@00:00" as datetime(6)) """
qt_sql41 """ select cast("20120212051" as datetime(6)) """
qt_sql42 """ select cast("2024-05-01T00:00XYZ" as datetime(6)) """
qt_sql43 """ select cast("2024-5-1T24:00" as datetime(6)) """
qt_sql44 """ select cast("2024-02-30" as datetime(6)) """
qt_sql45 """ select cast("2024-05-01T12:60" as datetime(6)) """
qt_sql46 """ select cast("2012-06-30T23:59:60" as datetime(6)) """
qt_sql47 """ select cast("2024-05-01T00:00+14:30" as datetime(6)) """
qt_sql48 """ select cast("2024-05-01T00:00+08:25" as datetime(6)) """
qt_sql49 """ select cast("9999-12-31 23:59:59.9999999" as datetime(6)) """
qt_sql50 """ select cast(" 2023-7-4T9-5-3.1Z " as datetime(6)) """
qt_sql51 """ select cast("99.12.31 23.59.59+05:30" as datetime(6)) """
qt_sql52 """ select cast("2000/01/01T00/00/00-230" as datetime(6)) """
qt_sql53 """ select cast("85 1 1T0 0 0. CST" as datetime(6)) """
qt_sql54 """ select cast("2024-02-29T23:59:59.999999 UTC" as datetime(6)) """
qt_sql55 """ select cast("70-01-01T00:00:00+14" as datetime(6)) """
qt_sql56 """ select cast("0023-1-1T1:2:3. -00:00" as datetime(6)) """
qt_sql57 """ select cast("2025/06/15T00:00:00.0-0" as datetime(6)) """
qt_sql58 """ select cast("2025/06/15T00:00:00.99999999999" as datetime(6)) """
qt_sql59 """ select cast("2024-02-29T23-59-60ZULU" as datetime(6)) """
qt_sql60 """ select cast("2024 12 31T121212.123456 America/New_York" as datetime(6)) """
qt_sql61 """ select cast("123.123" as datetime(6)) """
qt_sql62 """ select cast("12121" as datetime(6)) """

qt_sql63 """ select cast("2023-07-16T19:20:30.123+08:00" as datetime(3)) """
qt_sql64 """ select cast("2023-07-16T19+08:00" as datetime(3)) """
qt_sql65 """ select cast("2023-07-16T1920+08:00" as datetime(3)) """
qt_sql66 """ select cast("70-1-1T00:00:00-0000" as datetime(3)) """
qt_sql67 """ select cast("19991231T235959.5UTC" as datetime(3)) """
qt_sql68 """ select cast("2024-05-01T00:00Asia/Shanghai" as datetime(3)) """
qt_sql69 """ select cast("20231005T081530Europe/London" as datetime(3)) """
qt_sql70 """ select cast("20230105T081530 Europe/London" as datetime(3)) """
qt_sql71 """ select cast("85-12-25T000000gMt" as datetime(3)) """
qt_sql72 """ select cast("2024-05-01" as datetime(3)) """
qt_sql73 """ select cast("24-5-1" as datetime(3)) """
qt_sql74 """ select cast("2024-05-01 0:1:2.333" as datetime(3)) """
qt_sql75 """ select cast("2024-05-01 0:1:2." as datetime(3)) """
qt_sql76 """ select cast("20240501 01" as datetime(3)) """
qt_sql77 """ select cast("20230716 1920Z" as datetime(3)) """
qt_sql78 """ select cast("20240501T0000" as datetime(3)) """
qt_sql79 """ select cast("2024-12-31 23:59:59.9999999" as datetime(3)) """
qt_sql80 """ select cast("2025/06/15T00:00:00.99999999999999" as datetime(3)) """
qt_sql81 """ select cast("2025/06/15T00:00:00.9999987" as datetime(3)) """
qt_sql82 """ select cast("2025/06/15T00:00:00.99999849" as datetime(3)) """
qt_sql83 """ select cast("2020-12-12 13:12:12-03:00" as datetime(3)) """
qt_sql84 """ select cast("0023-01-01T00:00Z" as datetime(3)) """
qt_sql85 """ select cast("69-12-31" as datetime(3)) """
qt_sql86 """ select cast("70-01-01" as datetime(3)) """
qt_sql87 """ select cast("230102" as datetime(3)) """
qt_sql88 """ select cast("19230101" as datetime(3)) """
qt_sql89 """ select cast("120102030405" as datetime(3)) """
qt_sql90 """ select cast("20120102030405" as datetime(3)) """
qt_sql91 """ select cast("120102030405.999" as datetime(3)) """
qt_sql92 """ select cast("2024-05-01 0:1:2.333" as datetime(3)) """
qt_sql93 """ select cast("2023-07-16T19.123+08:00" as datetime(3)) """
qt_sql94 """ select cast("2024/05/01" as datetime(3)) """
qt_sql95 """ select cast("24012" as datetime(3)) """
qt_sql96 """ select cast("2411 123" as datetime(3)) """
qt_sql97 """ select cast("2024-05-01 01:030:02" as datetime(3)) """
qt_sql98 """ select cast("10000-01-01 00:00:00" as datetime(3)) """
qt_sql99 """ select cast("2024-0131T12:00" as datetime(3)) """
qt_sql100 """ select cast("2024-05-01@00:00" as datetime(3)) """
qt_sql101 """ select cast("20120212051" as datetime(3)) """
qt_sql102 """ select cast("2024-05-01T00:00XYZ" as datetime(3)) """
qt_sql103 """ select cast("2024-5-1T24:00" as datetime(3)) """
qt_sql104 """ select cast("2024-02-30" as datetime(3)) """
qt_sql105 """ select cast("2024-05-01T12:60" as datetime(3)) """
qt_sql106 """ select cast("2012-06-30T23:59:60" as datetime(3)) """
qt_sql107 """ select cast("2024-05-01T00:00+14:30" as datetime(3)) """
qt_sql108 """ select cast("2024-05-01T00:00+08:25" as datetime(3)) """
qt_sql109 """ select cast("9999-12-31 23:59:59.9999999" as datetime(3)) """
qt_sql110 """ select cast(" 2023-7-4T9-5-3.1Z " as datetime(3)) """
qt_sql111 """ select cast("99.12.31 23.59.59+05:30" as datetime(3)) """
qt_sql112 """ select cast("2000/01/01T00/00/00-230" as datetime(3)) """
qt_sql113 """ select cast("85 1 1T0 0 0. CST" as datetime(3)) """
qt_sql114 """ select cast("2024-02-29T23:59:59.999999 UTC" as datetime(3)) """
qt_sql115 """ select cast("70-01-01T00:00:00+14" as datetime(3)) """
qt_sql116 """ select cast("0023-1-1T1:2:3. -00:00" as datetime(3)) """
qt_sql117 """ select cast("2025/06/15T00:00:00.0-0" as datetime(3)) """
qt_sql118 """ select cast("2025/06/15T00:00:00.99999999999" as datetime(3)) """
qt_sql119 """ select cast("2024-02-29T23-59-60ZULU" as datetime(3)) """
qt_sql120 """ select cast("2024 12 31T121212.123456 America/New_York" as datetime(3)) """
qt_sql121 """ select cast("123.123" as datetime(3)) """
qt_sql122 """ select cast("12121" as datetime(3)) """

qt_sql123 """ select cast("2023-07-16T19:20:30.123+08:00" as datetime(0)) """
qt_sql124 """ select cast("2023-07-16T19+08:00" as datetime(0)) """
qt_sql125 """ select cast("2023-07-16T1920+08:00" as datetime(0)) """
qt_sql126 """ select cast("70-1-1T00:00:00-0000" as datetime(0)) """
qt_sql127 """ select cast("19991231T235959.5UTC" as datetime(0)) """
qt_sql128 """ select cast("2024-05-01T00:00Asia/Shanghai" as datetime(0)) """
qt_sql129 """ select cast("20231005T081530Europe/London" as datetime(0)) """
qt_sql130 """ select cast("20230105T081530 Europe/London" as datetime(0)) """
qt_sql131 """ select cast("85-12-25T000000gMt" as datetime(0)) """
qt_sql132 """ select cast("2024-05-01" as datetime(0)) """
qt_sql133 """ select cast("24-5-1" as datetime(0)) """
qt_sql134 """ select cast("2024-05-01 0:1:2.333" as datetime(0)) """
qt_sql135 """ select cast("2024-05-01 0:1:2." as datetime(0)) """
qt_sql136 """ select cast("20240501 01" as datetime(0)) """
qt_sql137 """ select cast("20230716 1920Z" as datetime(0)) """
qt_sql138 """ select cast("20240501T0000" as datetime(0)) """
qt_sql139 """ select cast("2024-12-31 23:59:59.9999999" as datetime(0)) """
qt_sql140 """ select cast("2025/06/15T00:00:00.99999999999999" as datetime(0)) """
qt_sql141 """ select cast("2025/06/15T00:00:00.9999987" as datetime(0)) """
qt_sql142 """ select cast("2025/06/15T00:00:00.99999849" as datetime(0)) """
qt_sql143 """ select cast("2020-12-12 13:12:12-03:00" as datetime(0)) """
qt_sql144 """ select cast("0023-01-01T00:00Z" as datetime(0)) """
qt_sql145 """ select cast("69-12-31" as datetime(0)) """
qt_sql146 """ select cast("70-01-01" as datetime(0)) """
qt_sql147 """ select cast("230102" as datetime(0)) """
qt_sql148 """ select cast("19230101" as datetime(0)) """
qt_sql149 """ select cast("120102030405" as datetime(0)) """
qt_sql150 """ select cast("20120102030405" as datetime(0)) """
qt_sql151 """ select cast("120102030405.999" as datetime(0)) """
qt_sql152 """ select cast("2024-05-01 0:1:2.333" as datetime(0)) """
qt_sql153 """ select cast("2023-07-16T19.123+08:00" as datetime(0)) """
qt_sql154 """ select cast("2024/05/01" as datetime(0)) """
qt_sql155 """ select cast("24012" as datetime(0)) """
qt_sql156 """ select cast("2411 123" as datetime(0)) """
qt_sql157 """ select cast("2024-05-01 01:030:02" as datetime(0)) """
qt_sql158 """ select cast("10000-01-01 00:00:00" as datetime(0)) """
qt_sql159 """ select cast("2024-0131T12:00" as datetime(0)) """
qt_sql160 """ select cast("2024-05-01@00:00" as datetime(0)) """
qt_sql161 """ select cast("20120212051" as datetime(0)) """
qt_sql162 """ select cast("2024-05-01T00:00XYZ" as datetime(0)) """
qt_sql163 """ select cast("2024-5-1T24:00" as datetime(0)) """
qt_sql164 """ select cast("2024-02-30" as datetime(0)) """
qt_sql165 """ select cast("2024-05-01T12:60" as datetime(0)) """
qt_sql166 """ select cast("2012-06-30T23:59:60" as datetime(0)) """
qt_sql167 """ select cast("2024-05-01T00:00+14:30" as datetime(0)) """
qt_sql168 """ select cast("2024-05-01T00:00+08:25" as datetime(0)) """
qt_sql169 """ select cast("9999-12-31 23:59:59.9999999" as datetime(0)) """
qt_sql170 """ select cast(" 2023-7-4T9-5-3.1Z " as datetime(0)) """
qt_sql171 """ select cast("99.12.31 23.59.59+05:30" as datetime(0)) """
qt_sql172 """ select cast("2000/01/01T00/00/00-230" as datetime(0)) """
qt_sql173 """ select cast("85 1 1T0 0 0. CST" as datetime(0)) """
qt_sql174 """ select cast("2024-02-29T23:59:59.999999 UTC" as datetime(0)) """
qt_sql175 """ select cast("70-01-01T00:00:00+14" as datetime(0)) """
qt_sql176 """ select cast("0023-1-1T1:2:3. -00:00" as datetime(0)) """
qt_sql177 """ select cast("2025/06/15T00:00:00.0-0" as datetime(0)) """
qt_sql178 """ select cast("2025/06/15T00:00:00.99999999999" as datetime(0)) """
qt_sql179 """ select cast("2024-02-29T23-59-60ZULU" as datetime(0)) """
qt_sql180 """ select cast("2024 12 31T121212.123456 America/New_York" as datetime(0)) """
qt_sql181 """ select cast("123.123" as datetime(0)) """
qt_sql182 """ select cast("12121" as datetime(0)) """

qt_sql183 """ select cast(cast(123.123 as bigint) as datetime(6)) """
qt_sql184 """ select cast(cast(20150102030405 as bigint) as datetime(6)) """
qt_sql185 """ select cast(cast(20150102030405.123456 as bigint) as datetime(6)) """
qt_sql186 """ select cast(cast(20151231235959.99999999999 as bigint) as datetime(6)) """
qt_sql187 """ select cast(cast(1000 as bigint) as datetime(6)) """

qt_sql188 """ select cast(cast(123.123 as double) as datetime(6)) """
qt_sql189 """ select cast(cast(20150102030405 as double) as datetime(6)) """
qt_sql190 """ select cast(cast(20150102030405.123456 as double) as datetime(6)) """
qt_sql191 """ select cast(cast(20151231235959.99999999999 as double) as datetime(6)) """
qt_sql192 """ select cast(cast(1000 as double) as datetime(6)) """

qt_sql193 """ select cast(cast(123.123 as decimal(27, 9)) as datetime(6)) """
qt_sql194 """ select cast(cast(20150102030405 as decimal(27, 9)) as datetime(6)) """
qt_sql195 """ select cast(cast(20150102030405.123456 as decimal(27, 9)) as datetime(6)) """
qt_sql196 """ select cast(cast(20151231235959.99999999999 as decimal(27, 9)) as datetime(6)) """
qt_sql197 """ select cast(cast(1000 as decimal(27, 9)) as datetime(6)) """

qt_sql198 """ select cast(cast("2012-02-05" as date) as datetime(6)) """
qt_sql199 """ select cast(cast("2012-02-05" as date) as datetime(0)) """

sql """ select cast(cast("500:00:00" as time(1)) as datetime(6)) """
sql """ select cast(cast("23:59:59" as time(3)) as datetime(0)) """

qt_sql200 """ select cast(cast("2020-12-12 00:00:00.123" as datetime(3)) as datetime(6)) """
qt_sql201 """ select cast(cast("2020-12-12 00:00:00.123456" as datetime(6)) as datetime(3)) """
qt_sql202 """ select cast(cast("2020-12-12 00:00:00.99666" as datetime(6)) as datetime(2)) """
qt_sql203 """ select cast(cast("9999-12-31 23:59:59.999999" as datetime(6)) as datetime(5)) """

qt_sql204 """ select cast('9999-12-31 23:59:59.999999 +00:00' as datetime(6)) """
testFoldConst("select cast('9999-12-31 23:59:59.999999 +00:00' as datetime(6))")
qt_sql205 """ select cast('0000-01-01 00:00:00+12:00' as datetime(6)); """
testFoldConst("select cast('0000-01-01 00:00:00+12:00' as datetime(6));")

    sql "set debug_skip_fold_constant = false"

    // for strict mode
    sql "set enable_strict_cast = true"
    qt_strict_1 "select cast('2023/6/10 3:55:33' as datetime(6)) "
    testFoldConst("select cast('2023/6/10 3:55:33' as datetime(6)) ")
    qt_strict_1 "select cast('2023/6/10 3:55:33' as date) "
    testFoldConst("select cast('2023/6/10 3:55:33' as date) ")
    test {
        sql "select cast('9999-12-31 23:59:59.999999 +00:00' as datetime(6));"
        exception "out of range"
    }
    test {
        sql "select cast('9999-12-31 23:59:59.999999 +05:00' as datetime(6));"
        exception "out of range"
    }
    test {
        sql "select cast('0000-01-01 00:00:00+12:00' as datetime(6));"
        exception "out of range"
    }
}